package com.controller;

import com.bean.PlanInfo;
import com.bean.other.MetaInfo;
import com.bean.other.RequestData;
import com.bean.other.ResponseData;
import com.github.pagehelper.PageInfo;
import com.service.impl.PlanInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("api/plan_info")
public class PlanInfoController {

    @Autowired
    private PlanInfoService planInfoService;

    @PostMapping("add")
    public ResponseEntity<ResponseData> add(@RequestBody PlanInfo planInfo){
        planInfoService.insert(planInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
        //return  ResponseEntity.status(HttpStatus.NOT_FOUND).body("没有找到");//直接设置响应头
    }

    @PostMapping("edit")
    public ResponseEntity<ResponseData> edit(@RequestBody PlanInfo planInfo){
        planInfoService.update(planInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @DeleteMapping("del/{id}")
    public ResponseEntity<ResponseData> del(@PathVariable String id){
        PlanInfo planInfo=new PlanInfo();
        planInfo.setId(id);
        planInfoService.delete(planInfo);
        return ResponseEntity.ok(ResponseData.success("成功"));
    }

    @GetMapping("get")
    public ResponseEntity<ResponseData> get(@RequestParam("id")String id){
        PlanInfo planInfo=planInfoService.get(id);
        return ResponseEntity.ok(ResponseData.success(planInfo));
    }

    @PostMapping("list")//@RequestBody只能使用一次，使用的是流，用过后就不能用了。
    public ResponseEntity<ResponseData> list(@RequestBody RequestData<PlanInfo> requestData){
        PlanInfo planInfo=requestData.getObj();
        planInfo.setCreator(requestData.getUserid());
        MetaInfo metaInfo =requestData.getMetaInfo();
        PageInfo<PlanInfo> pageInfo=planInfoService.select(planInfo,metaInfo);
        metaInfo.setPages(pageInfo.getPages());
        metaInfo.setTotal((int) pageInfo.getTotal());
        return ResponseEntity.ok(ResponseData.success(pageInfo.getList(),metaInfo));
    }

    @PostMapping("sort")
    public ResponseEntity<ResponseData> sort(@RequestBody RequestData<List<PlanInfo>> requestData){
        List<PlanInfo> list=requestData.getObj();
        MetaInfo metaInfo=requestData.getMetaInfo();
        String message=planInfoService.sort(list,metaInfo);
        /*for(PlanInfo planInfo:list){
            System.out.println(planInfo.toString());
        }*/
        return ResponseEntity.ok(ResponseData.success("成功"));
    }


}
